var mongoose = require('./db')



// ZT
const userSchema = new mongoose.Schema({
  username: { type: String, required: true, unique: true },
  password: { type: String, sparse: true},
  phone: { type: String, sparse: true, unique: true },
  email: { type: String, sparse: true, unique: true },
  avatar: { type: String, unique: false,default: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.V-0gtFJqCf_iAWBxMd874AAAAA?rs=1&pid=ImgDetMain' },
  giteeId: { type: String, sparse: true },
  accessToken: {type:String, sparse: true},
})
const RoleSchema = mongoose.Schema({
    role_name: String,
    x_id: String,
    description: String,
})
const PermissionSchema = mongoose.Schema({
    permission_name: String,
    description: String,
})
const UserRoleSchema = mongoose.Schema({
    user_id: String,
    role_id: String,
})
const RolePermissionSchema = mongoose.Schema({
    role_id: String,
    permission_id: String,
})
const verificationCodeSchema = new mongoose.Schema({
    phone: {
        type: String,
        required: true,
        unique: true
    },
    code: {
        type: String,
        required: true
    },
    expirationTime: {
        type: Date,
        required: true
    }
});

const VerificationCode = mongoose.model('VerificationCode', verificationCodeSchema);
const User = mongoose.model('Users', userSchema)
const Role = mongoose.model('Role', RoleSchema)
const Permission = mongoose.model('Permission', PermissionSchema)
const UserRole = mongoose.model('UserRole', UserRoleSchema)
const RolePermission = mongoose.model('RolePermission', RolePermissionSchema)
//cws
const UsersSchema = new mongoose.Schema({
  username: { type: String, unique: true },
  password: String,
  email: String,
  online: { type: Boolean, default: false }
});
const Useres = mongoose.model('Useres', UsersSchema);


const MessageSchema = new mongoose.Schema({
    from: { type: mongoose.Schema.Types.ObjectId, ref: 'Useres', required: true },
    to: { type: mongoose.Schema.Types.ObjectId, ref: 'Useres', required: true },
    content: { type: String, required: true },
    timestamp: { type: Date, default: Date.now }
  });
  
  const Messages = mongoose.model('Message', MessageSchema);


//cxf
const HistorySchema = new mongoose.Schema({
    // project_id:{
    //     type: mongoose.Schema.Types.ObjectId,
    //     ref: 'Project'
    // },
    project_id:Number,//关联项目的外键
    content: Array,//二进制数据
    version: Number,//当前版本号
    created_at:String,//创建时间
    updated_at:String//最后更新时间
});
const History = mongoose.model('History', HistorySchema);

//cyw
const taskSchema = new mongoose.Schema({
    title: String,
    description: String,
    assignee: String,
    dueDate: String,
    tags: String,
    status: { type: String, default: 'todo' },
    project_id: String,
    completed_at: { type: Date, default: null }
});
const Task = mongoose.model('Task', taskSchema)
const commentSchema = new mongoose.Schema({
    id: { type: String, unique: true },
    content: String,
    task_id: String,
    user_id: String,
    created_at: { type: Date, default: Date.now }
});

const Comment = mongoose.model('Comment', commentSchema);

const archivedTaskSchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: String,
    description: String,
    assignee: String,
    dueDate: Date,
    tags: String,
    status: String,
    project_id: String,
    completed_at: Date,
    // 可以根据需要添加更多字段，比如归档时间
    archived_at: { type: Date, default: Date.now } 
  });
  
  const ArchivedTask = mongoose.model('ArchivedTask', archivedTaskSchema);
//yl
const wenjianSchema = new mongoose.Schema({
    name: String,
    weizhi_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'weizhi'
    }
})
const weizhiSchema = new mongoose.Schema({
    name: String
})
const wenjianModel = mongoose.model('wenjian', wenjianSchema)
const weizhiModel = mongoose.model('weizhi', weizhiSchema)
//ZT
module.exports = {
User,
Role,
Permission,
UserRole,
RolePermission,
VerificationCode,
//cws
Messages,
Useres,

//cxf
History,
    Task,
    Comment,

    ArchivedTask,
    //yl
    wenjianModel,
    weizhiModel,
}